package cn.life.business.warehouse.controller;

import cn.life.business.warehouse.entity.po.Warehouse;
import cn.life.business.warehouse.entity.po.WarehouseDetails;
import cn.life.business.warehouse.entity.vo.WarehouseVo;
import cn.life.business.warehouse.service.WarehouseService;
import cn.life.common.entity.model.CMessage;
import cn.life.common.entity.model.MyUser;
import cn.life.common.entity.model.MyUserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author 王振宇
 * @Description:
 * @Date 2018-07-31 14:41:57
 */
@RestController
@RequestMapping("warehouse")
public class WarehouseController {

    @Autowired
    private WarehouseService warehouseService;

    /**
     * @Description:  自动添加项
     * @param  warehouse,myUserInfo
     * @return
     */
    public Warehouse getOpr(Warehouse warehouse, MyUserInfo myUserInfo){
        if(warehouse.getProjectId() == null || warehouse.getProjectId().equals("")){
            warehouse.setProjectId(myUserInfo.getProjectId());
        }
        warehouse.setOprUserId(myUserInfo.getUserId());
        warehouse.setOprUserName(myUserInfo.getUserName());
        warehouse.setOprDate(new Date());
        return warehouse;
    }

    /**
     * @Description:  新增仓库记录
     * @param
     * @return:
     * @Author 王振宇
     * @Date 2018/07/31
     */
    @PostMapping(value = "/addwarehouse")
    public CMessage addWarehouse(@RequestBody Warehouse warehouse,@MyUser MyUserInfo myUserInfo){
        getOpr(warehouse,myUserInfo);
      return warehouseService.addWarehouse(warehouse);
    }

    /**
     * @Description:  删除仓库记录
     * @param
     * @return:
     * @Author 王振宇
     * @Date 2018/08/01
     */
    @PostMapping(value = "/deletewarehouse")
    public CMessage deleteWarehouse(@RequestBody Warehouse warehouse,@MyUser MyUserInfo myUserInfo){
        getOpr(warehouse,myUserInfo);
        return warehouseService.deletewarehouse(warehouse);
    }

    /**
     * @Description:  修改仓库主表记录
     * @param
     * @return:
     * @Author 王振宇
     * @Date 2018/08/01
     */
    @PostMapping(value = "/updatewarehouse")
    public CMessage updateWarehouse(@RequestBody Warehouse warehouse){
        return warehouseService.updateWarehouse(warehouse);
    }

    /**
     * @Description:  修改仓库明细记录
     * @param
     * @return:
     * @Author 王振宇
     * @Date 2018/08/01
     */
    @PostMapping(value = "/updatewarehousedetails")
    public CMessage updateWarehouseDetails(@RequestBody WarehouseDetails warehouseDetails){
        return warehouseService.updateWarehouseDetails(warehouseDetails);
    }

    /**
     * @Description:  查询仓库记录
     * @param
     * @return:
     * @Author 王振宇
     * @Date 2018/08/01
     */
    @PostMapping(value = "/selectwarehouse")
    public CMessage selectWarehouse(@RequestBody WarehouseVo warehouseVo,@MyUser MyUserInfo myUserInfo){
        if(warehouseVo.getProjectId() == null){
            warehouseVo.setProjectId(myUserInfo.getProjectId());
        }
        return warehouseService.selectWarehouse(warehouseVo);
    }

    /**
     * @Description:  查询仓库记录
     * @param
     * @return:
     * @Author 王振宇
     * @Date 2018/08/01
     */
    @PostMapping(value = "/warehouseDetail")
    public CMessage queryWarehouseDetail(@RequestBody WarehouseVo warehouseVo){
        return warehouseService.queryWarehouseDetail(warehouseVo);
    }

    /**
     * @Description:  查询仓库记录
     * @param
     * @return:
     * @Author 王振宇
     * @Date 2018/08/01
     */
    @PostMapping(value = "/getwarehouse")
    public CMessage getWarehouse(@RequestBody WarehouseVo warehouseVo){
        return warehouseService.getWarehouse(warehouseVo);
    }

    /**
     * @Description:  查询仓库记录
     * @param
     * @return:
     * @Author 王振宇
     * @Date 2018/08/01
     */
    @PostMapping(value = "/getwarehousebystate")
    public CMessage getWarehouseByState(@RequestBody WarehouseVo warehouseVo,@MyUser MyUserInfo myUserInfo){
        /*if(warehouseVo.getProjectId() == null){
            warehouseVo.setProjectId(myUserInfo.getProjectId());
        }*/
        return warehouseService.getWarehouseByState(warehouseVo);
    }

}
